using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using OpenFlashChartLib;
using OpenFlashChartLib.Charts;
using OpenFlashChartLib.Controls;

namespace WebDemo
{
    public partial class DemoJQuery : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlGraphType.Items.Add(new ListItem("Bar chart", ((int)ChartType.Bar).ToString()));
                ddlGraphType.Items.Add(new ListItem("Filled Bar", ((int)ChartType.FilledBar).ToString()));
                ddlGraphType.Items.Add(new ListItem("Glass chart", ((int)ChartType.GlassBar).ToString()));
                ddlGraphType.Items.Add(new ListItem("3D chart", ((int)ChartType.Bar3D).ToString()));
                ddlGraphType.Items.Add(new ListItem("Pie chart", ((int)ChartType.Pie).ToString()));
                ddlGraphType.Items.Add(new ListItem("Horizontal Bar", ((int)ChartType.HorizontalBar).ToString()));
                ddlGraphType.Items.Add(new ListItem("Sketch Bar", ((int)ChartType.Sketch).ToString()));
                ddlGraphType.Items.Add(new ListItem("Lines", ((int)ChartType.Line).ToString()));
                ddlGraphType.Items.Add(new ListItem("Dot Lines", ((int)ChartType.DotLine).ToString()));
                ddlGraphType.Items.Add(new ListItem("Hollow Lines", ((int)ChartType.HollowLine).ToString()));
            }
            ofcTest.BuildChart();
            ofcTest.ScriptRegister += new RegisterScriptEventHandler(ofcTest_ScriptRegister);
        }

        void ofcTest_ScriptRegister(object sender, RegisterScriptEventArgs e)
        {
            ScriptManager.RegisterStartupScript(Page, GetType(), "embed", e.ScriptBody, true);
        }

        protected void ofcTest_DrawChart(object sender, DrawChartEventArgs e)
        {
            var graph = new Graph();
            graph.Title = new Title("One, two, three");
            ChartBase chart = CreateChart();
            

            if (chart != null)
            {
                var r = new Random();
                for (var i = 0; i < 5; i++)
                {
                    ILabelValue val = chart.NewValue("label " + i, r.NextDouble()*30.0);
                    chart.Values.Add(val);
                }
                graph.AddElement(chart);
            }

            e.Graph = graph;
        }

        private ChartBase CreateChart()
        {
            ChartType type = (ChartType)Convert.ToInt32(ddlGraphType.SelectedValue);
            ChartBase result = null;
            switch (type)
            {
                case ChartType.Bar:
                    result = new Bar();
                    break;
                case ChartType.FilledBar:
                    result = new BarFilled(){OutlineColour = "#010101"};
                    break;
                case ChartType.GlassBar:
                    result = new BarGlass();
                    break;
                case ChartType.Sketch:
                    result = new BarSketch() {OutlineColour = "#3300FF"};
                    break;
                case ChartType.Bar3D:
                    result = new Bar3D();
                    break; 
                case ChartType.Line:
                    result = new Line();
                    break;
                case ChartType.DotLine:
                    result = new LineDot();
                    break;
                case ChartType.HollowLine:
                    result = new LineHollow();
                    break;
                case ChartType.Pie:
                    result = new Pie();
                    break;
                case ChartType.HorizontalBar:
                    result = new HBar();
                    break;
            }
            if (result != null)
            {
                result.Fillalpha = 0.4;
                result.Fontsize = 10.0;
                result.Text = "Some text on chart";
            }
            return result;
        }
    }
}
